package com.app.megaprosoft.modyimcarcare;

import java.util.List;

import com.megaprosoft.modyimcarcare.R;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageButton;
import android.widget.TextView;

public class CarCare_Adapter_ArGroupMain extends ArrayAdapter<CarCare_List_ArGroup> {

	protected static final String LOG_TAG = CarCare_Adapter_ArGroupMain.class.getSimpleName();
	
	private List<CarCare_List_ArGroup> items;
	private int layoutResourceId;
	private Context context;

	public CarCare_Adapter_ArGroupMain(Context context, int layoutResourceId, List<CarCare_List_ArGroup> items) {
		super(context, layoutResourceId, items);
		this.layoutResourceId = layoutResourceId;
		this.context = context;
		this.items = items;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View row = convertView;
		CarCare_ArGroupHolder holder = null;
		 
		LayoutInflater inflater = ((Activity) context).getLayoutInflater();
		row = inflater.inflate(layoutResourceId, parent, false);
		
		holder = new CarCare_ArGroupHolder();
		holder.list_argroup = items.get(position);
		
		holder.check = (CheckBox)row.findViewById(R.id.argroup_check);
		
		holder.name = (TextView)row.findViewById(R.id.argroup_name);
		
		setOnCheckedChangeListener(holder);

		row.setTag(holder);

		setupItem(holder);
		return row;
	}

	private void setupItem(CarCare_ArGroupHolder holder) {
		holder.check.setChecked(holder.list_argroup.isSelected());
		holder.name.setText(holder.list_argroup.getName());
		
	}

	public static class CarCare_ArGroupHolder {
		CarCare_List_ArGroup list_argroup;
		CheckBox check;
		TextView name;
	}
	
	private void setOnCheckedChangeListener(final CarCare_ArGroupHolder holder) {
		holder.check
	     .setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
	       @Override
	      public void onCheckedChanged(CompoundButton view,
	        boolean isChecked) {
//	       int getPosition = (Integer) view.getTag();
//	       items.get(getPosition).setSelected(view.isChecked());
	    	   holder.list_argroup.setSelected(true);
	       }
	     });
		holder.name.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
//				Carcare_getCar getcar = new Carcare_getCar();
//				getcar.setArGroupMainOID(holder.list_argroup.getArGroupMainOID());
//				getcar.UpdateSelect_ArGroupMain();
//				if(holder.list_argroup.isSelected()){
//					holder.list_argroup.setSelected(false);
//					
//					
//				}else{
//					holder.list_argroup.setSelected(true);
//				} 
				holder.list_argroup.setSelected(true);
				notifyDataSetChanged();
			}
		});
	}
//
//	private void setNumberTextListeners(final CarCare_PaymentHolder holder) {
//		holder.number.addTextChangedListener(new TextWatcher() {
//
//			@Override
//			public void onTextChanged(CharSequence s, int start, int before, int count) {
//				try{
//					holder.carcarePayment.setNumber(Integer.valueOf(s.toString()));
//				}catch (NumberFormatException e) {
//					Log.e(LOG_TAG, "error reading double value: " + s.toString());
//				}
//			}
//
//			@Override
//			public void beforeTextChanged(CharSequence s, int start, int count, int after) { }
//
//			@Override
//			public void afterTextChanged(Editable s) { }
//		});
//	}
//	private OnClickListener Itemclick = new OnClickListener() {
//        public void onClick(View v) {
//        	holder = get
//        	v.check.
//        }
// }; 
}
